#ifndef _DSWSERVERCENTER_H
#define _DSWSERVERCENTER_H

#include "base.h"
#include "dswserversession.h"
#include "dswwanmsgsession.h"
#include "dswfwdsession.h"

typedef struct{
	EN_MISSION_TYPE nMissionType;
	char sHost[64];
	int nPort;

	char sIp[32];
	void* pParam;
} ST_HOST_T, *LPST_HOST_T;

class CDswservercenter
{
public:
	CDswservercenter(LPST_ENV_T pEnv);
	virtual ~CDswservercenter();
public:
	static void cbOnMissionNotify(LPST_MISSION_NOTIFY_T nNotify, void* pCaller){((CDswservercenter*)pCaller)->OnMissionNotify(nNotify);};
	bool IsLinkServer(){return (abs(time(NULL) - m_nRegistLastAnsyTime) < LINK_SERVER_TIMEOUT);};
	void SetCaptureResult(LPST_CAPTURE_RESULT_T pResult);

	static void cbLocalTransFunc(void* pLocalTransParam, char* pData, int nLen, void* pCaller){((CDswservercenter*)pCaller)->LocalTransFunc((LPST_LOCAL_TRANS_T)pLocalTransParam, pData, nLen);};
	void LocalTransFunc(LPST_LOCAL_TRANS_T pLocalTransParam, char* pData, int nLen);
private:
	static void cbOnCreateCenterServerSession(int fd, short events, void* ctx){((CDswservercenter*)ctx)->OnCreateCenterServerSession(fd, events);};
	void OnCreateCenterServerSession(int fd, short events);
	static void cbOnCreateRegisterServerSession(int fd, short events, void* ctx){((CDswservercenter*)ctx)->OnCreateRegisterServerSession(fd, events);};
	void OnCreateRegisterServerSession(int fd, short events);
	static void cbOnCreateAlarmServerSession(int fd, short events, void* ctx){((CDswservercenter*)ctx)->OnCreateAlarmServerSession(fd, events);};
	void OnCreateAlarmServerSession(int fd, short events);
	static void cbOnCreateMsgServerSession(int fd, short events, void* ctx){((CDswservercenter*)ctx)->OnCreateMsgServerSession(fd, events);};
	void OnCreateMsgServerSession(int fd, short events);

	void OnMissionNotify(LPST_MISSION_NOTIFY_T pNotify);

	void StartFwdMission(CMarkup* pxml);
	bool AddFwdMissionNode(CDswfwdsession* pSession);
	bool AppendFwdMissionNode(CMarkup* pxml);
	void RemoveFwdMissionNode(CDswfwdsession* pSession);

	bool StartDNSRequest(char* pHost, int nPort, EN_MISSION_TYPE nMissionType);
	static void cbOnDnsRequest(int errcode, struct evutil_addrinfo *addr, void *ptr){((CDswservercenter*)(((LPST_HOST_T)ptr)->pParam))->OnDnsRequest(errcode, addr, ptr);};
	void OnDnsRequest(int errcode, struct evutil_addrinfo *addr, void *ptr);
private:
	LPST_ENV_T m_pEnv;
	
	timer_param_t m_nTask_CreateCenterServerSession, m_nTask_CreateRegisterServerSession,
		m_nTask_CreateAlarmServerSession, m_nTask_CreateMsgServerSession;
	CDswserversession *m_pCenterServerSession, *m_pRegisterServerSession,
		*m_pAlarmServerSession;
	CDswWanMsgSession *m_pMsgServerSession;
	
	t_svr_param m_regSvrParam, m_alarmSvrParam, m_fwdSvrParam, m_msgSvrParam;

	int m_nRegistCount;
	time_t m_nRegistLastAnsyTime;

	ST_LIST_CAPTURE_RESULT_T m_result_list;

	CDswfwdsession* m_pFwdList[MAX_CHANNEL][MAX_V_BITSTREAM_NUM];
	pthread_mutex_t m_nFwdList_lock;

	ST_HOST_T m_nCenterServer, m_nRegisterServer, m_nAlarmServer, m_nMsgServer, m_nFwdServer;
};

#endif

